<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      h2 {
        text-align: center;
      }

      .box {
        width: 600px;
        margin: 50px auto;
        display: flex;
        font-size: 25px;
        line-height: 40px;
      }

      .qs {
        width: 450px;
        height: 40px;
        color: red;
      }

      .btns {
        text-align: center;
      }

      .btns button {
        width: 120px;
        height: 35px;
        margin: 0 50px;
      }
    </style>
  </head>

  <body>
    <h2>随机点名</h2>
    <div class="box">
      <span>名字是：</span>
      <div class="qs">这里显示姓名</div>
    </div>
    <div class="btns">
      <button class="start">开始</button>
      <button class="end">结束</button>
    </div>
    <div>
      <h2>中奖名单</h2>
      <div id="names">？？？</div>
    </div>
    <script>
      // 1. 前期准备
      // 数据数组
      const arr = [
        "马嘉祺",
        "丁程鑫",
        "宋亚轩",
        "刘耀文",
        "张真源",
        "严浩翔",
        "贺峻霖",
      ];
      //显示名字的对象
      const nameBox = document.querySelector(".qs");
      const start = document.querySelector(".start");
      const end = document.querySelector(".end");
      let n = 0;
      let i = -1;
      // 2.给开始按钮添加事件监听
      start.addEVentiListener("click", function () {
        n = setInterval(function () {
          i = Math.floor(Math.random() * arr.length);
          nameBox.innerHTML = arr[i];
        }, 300);
        if (arr.length === 1) {
          start.disabled = end.disabled = true;
        }
      });

      let str = "";
      // 3.结结束按钮的添加事件监听
      end.addEventListener("click", function () {
        clearInterval(n);
        const name = arr.splice(i, 1);
        console.log(name);
        str += name[0] + "、";
        document.querySelector("#names").innerHTML = str;
      });
    </script>
  </body>
</html>
